using System;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;

namespace My.IdlConverter.Core.Util
{
    public static class StringUtil
    {
        public static string[] SpecialPluralTailChars = 
        {
            "ch"
        };

        public static string ChangeToSingular(string plural)
        {
            plural = plural.Trim();
            int endPos = plural.Length - 1;
            if (plural[endPos] == 's')
            {
                if (plural[endPos - 1] == 'e')
                {
                    if (Array.IndexOf<string>(SpecialPluralTailChars, new string(new char[] { plural[endPos - 3], plural[endPos - 2] })) != -1)
                    {
                        return plural.Remove(endPos - 1, 2);
                    }
                }
                else
                {
                    return plural.Remove(endPos, 1);
                }
            }
            return plural;
        }

        public static void CapitalString(ref string value)
        {
            char firstChar = value[0];
            if (Char.IsUpper(firstChar) == false)
                value = value.Replace(firstChar, Char.ToUpper(firstChar));
        }

        public static string UpperString(string value)
        {
            char[] chars = value.ToCharArray();
            StringBuilder sb = new StringBuilder();
            foreach (char nextChar in chars)
            {
                sb.Append(Char.ToUpper(nextChar));
            }
            return sb.ToString();
        }

        public static bool RoughMatch(string shortString, string longString)
        {
            return false;
        }

        public static StringBuilder FillBuffer(StringBuilder buffer, StringBuilder filledContent)
        {
            if (filledContent.Length > 0)
                buffer.Append(filledContent);

            return buffer;
        }

        public static StringBuilder FillBuffer(StringBuilder buffer, string filledContent)
        {
            if (filledContent.Length > 0)
                buffer.Append(filledContent);

            return buffer;
        }

        public static string[] SplitStl(string stlTypeString)
        {
            int begPos = stlTypeString.IndexOf('<');
            int endPos = stlTypeString.IndexOf('>');
            int count = endPos - begPos - 1;
            string typeString = stlTypeString.Substring(begPos + 1, count);
            return typeString.Split(',');
        }

        public static string RemoveRange(string source, char beginChar, char endChar)
        {
            int begPos = source.IndexOf(beginChar);
            int endPos = source.LastIndexOf(endChar);
            if (begPos == -1 || endPos == -1 || begPos==endPos)
            {
                Debug.Assert(false);
                return source;
            }
            if (begPos > endPos)
            {
                int tmp = begPos;
                begPos = endPos;
                endPos = tmp;
            }

            int count = endPos-begPos+1;
            return source.Remove(begPos, count);
        }

        public static string FormatIndent0(string format)
        {
            return format;
        }

        public static string FormatIndent0(string format, params object[] objs)
        {
            return String.Format(format,objs);
        }

        public static string FormatIndent1(string format)
        {
            return GlobalConst.DefaultIndent + format;
        }

        public static string FormatIndent1(string format,params object[] objs)
        {
            return String.Format(GlobalConst.DefaultIndent+format, objs);
        }

        public static string FormatIndent2(string format, params object[] objs)
        {
            return String.Format(GlobalConst.DefaultIndent+GlobalConst.DefaultIndent + format, objs);
        }

        public static string FormatIndent3(string format)
        {
            return GlobalConst.DefaultIndent + GlobalConst.DefaultIndent + GlobalConst.DefaultIndent + format;
        }

        public static string FormatIndent3(string format, params object[] objs)
        {
            return String.Format(GlobalConst.DefaultIndent + GlobalConst.DefaultIndent + GlobalConst.DefaultIndent + format, objs);
        }

        public static string FormatIndent4(string format)
        {
            return GlobalConst.DefaultIndent + GlobalConst.DefaultIndent + GlobalConst.DefaultIndent + GlobalConst.DefaultIndent + format;
        }

        public static string FormatIndent4(string format, params object[] objs)
        {
            return String.Format(GlobalConst.DefaultIndent + GlobalConst.DefaultIndent + GlobalConst.DefaultIndent + GlobalConst.DefaultIndent+  format, objs);
        }
    }
}
